package com.SouthernWall_404.AgeSuitcase.Client.Gui.Buttons;

import com.SouthernWall_404.AgeSuitcase.Common.API.GuiSoundHelper;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

public class TransformableImageButton extends ImageButton {
    private float scaleX = 1.0f;
    private float scaleY = 1.0f;
    private float rotation = 0.0f;

    public Component message=Component.empty();
    public int color=0x000000;
    public Font font= Minecraft.getInstance().font;


    public TransformableImageButton(int x, int y, int length,int height, int textureStartX, int textureStartY, int move, float renderX, float renderY, ResourceLocation resourceLocation, int textureX, int textureY, OnPress onPress) {
        super(x, y, length, height, textureStartX, textureStartY, move,resourceLocation, textureX, textureY, onPress);
        this.scaleX = renderX/this.width;
        this.scaleY = renderY/this.height;
    }

    public TransformableImageButton(int x, int y, int width, int height, int textureStartX, int textureStartY, int move, float renderX,ResourceLocation resourceLocation, int textureX, int textureY, OnPress onPress) {
        super(x, y, width, height, textureStartX, textureStartY, move, resourceLocation, textureX, textureY, onPress);
        this.scaleX = renderX/this.width;
        this.scaleY = (int)renderX*((float)height/width)/this.height ;
    }



    @Override
    public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) {
        if (!this.visible) return;

        renderButton(graphics,mouseX,mouseY,partialTicks);
    }


    private void renderButton(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks)
    {
        PoseStack poseStack=graphics.pose();
        // 保存当前变换状态
        poseStack.pushPose();
        poseStack.scale(this.scaleX,this.scaleY,1f);
        poseStack.translate(this.getX()/this.scaleX-this.getX(),this.getY()/this.scaleY-this.getY(),0);
        // 渲染按钮
        super.render(graphics, 0, 0, partialTicks);
        // 恢复变换状态
        poseStack.popPose();
    }

    @Override
    public boolean isMouseOver(double mouseX, double mouseY) {
        // 简化处理：使用原始矩形区域进行检测
        // 对于更精确的检测，需要实现更复杂的几何变换
        return mouseX >= this.getX() &&
                mouseY >= this.getY() &&
                mouseX < (this.getX() + this.width* scaleX )&&
                mouseY < (this.getY() + this.height* scaleY);
    }

    @Override
    public boolean mouseClicked(double p_93641_, double p_93642_, int p_93643_) {



        if (this.active && this.visible) {
            if (this.isValidClickButton(p_93643_)) {
                boolean flag = this.clicked(p_93641_, p_93642_);
                if (flag) {
                    GuiSoundHelper.playButtonClick();
                    this.onClick(p_93641_, p_93642_);
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    @Override
    protected boolean clicked(double mouseX, double mouseY) {
        return this.active && this.visible && mouseX >= (double)this.getX() && mouseY >= (double)this.getY() && mouseX < (double)(this.getX() + this.width* scaleX) && mouseY < (double)(this.getY() + this.height* scaleY);
    }

}
